from blacklist import Blacklist


def detect_api_type(api_dict, call_name):
    for type, api_list in api_dict.items():
        if call_name in api_list:
            return type
    return ''


def detect_api(call):
    blacklist = Blacklist()
    sources = blacklist.sources
    sinks = blacklist.sinks
    sourcesinks = blacklist.sourcesinks
    dangers = blacklist.dangers
    source_type = detect_api_type(sources, call)
    if source_type:
        return 'Source', source_type
    sink_type = detect_api_type(sinks, call)
    if sink_type:
        return 'Sink', sink_type
    sourcesink_type = detect_api_type(sourcesinks, call)
    if sourcesink_type:
        return 'Sourcesink', sourcesink_type
    danger_type = detect_api_type(dangers, call)
    if danger_type:
        return 'Danger', danger_type
    return '', ''


def get_type_number(category, type):
    category_num_dict = {'Source': 'A', 'Sink': 'B', 'Sourcesink': 'C', 'Danger': 'D'}
    type_num_dict = {'network': '01', 'file': '02', 'process': '03', 'code': '04', 'input': '05', 'account': '06',
                     'settings': '07', 'obfuscation': '08'}
    category_num = category_num_dict[category]
    type_num = '00'
    for api_type, temp_type_num in type_num_dict.items():
        if api_type in type.lower():
            type_num = temp_type_num
            break
    return category_num + type_num


def detect_call(call):
    api_category, api_type = detect_api(call)
    if not api_category:
        return ''
    api_num = get_type_number(api_category, api_type)
    return api_num


def detect_dangercalls(dangercalls):
    source_apis = []
    sink_apis = []
    for dangercall in dangercalls:
        type_num = dangercall[1:]
        # if dangercall.startswith('D'):
        #     return True

        # if dangercall.startswith('A') or dangercall.startswith('C'):
        #     source_apis.append(type_num)
        #     if type_num == '02' and '01' in sink_apis:
        #         return True
        #     if type_num == '01' and ('03' in sink_apis or '04' in sink_apis):
        #         return True
        # if dangercall.startswith('B') or dangercall.startswith('C'):
        #     sink_apis.append(type_num)
        #     if type_num == '01' and '02' in source_apis:
        #         return True
        #     if (type_num == '03' or type_num == '04') and '01' in source_apis:
        #         return True
        # if dangercall=='D02' or dangercall=='D03':
        #     return True
        if dangercall.startswith('A') or dangercall.startswith('C'):
            source_apis.append(type_num)
            if (type_num == '02' or type_num == '06' or type_num == '07') and ('01' in sink_apis or '05' in sink_apis):
                return True
            if (type_num == '01' or type_num == '05' or type_num == '08') and ('03' in sink_apis or '04' in sink_apis):
                return True
        if dangercall.startswith('B') or dangercall.startswith('C') or dangercall.startswith('D'):
            sink_apis.append(type_num)
            if (type_num == '01' or type_num == '05') and (
                    '02' in source_apis or '06' in source_apis or '07' in source_apis):
                return True
            if (type_num == '03' or type_num == '04' or type_num == '08') and (
                    '01' in source_apis or '05' in source_apis or '08' in sink_apis):
                return True

    # for dangercall in dangercalls:
    #     type_num = dangercall[1:]
    #     if dangercall.startswith('D'):
    #         return True
    #     if dangercall.startswith('A') or dangercall.startswith('C'):
    #         source_apis.append(type_num)
    #         if (type_num == '02' or type_num == '06' or type_num == '07') and '01' in sink_apis:
    #             return True
    #         if type_num == '01' and ('03' in sink_apis or '04' in sink_apis or '08' in sink_apis):
    #             return True
    #     if dangercall.startswith('B') or dangercall.startswith('C'):
    #         sink_apis.append(type_num)
    #         if type_num == '01' and '02' in source_apis:
    #             return True
    #         if (type_num == '03' or type_num == '04' or type_num == '08') and '01' in source_apis:
    #             return True
    return False


# def detect_dangercalls(dangercalls):
#     source_apis = []
#     sink_apis = []
#     danger_flag = False
#     for dangercall in dangercalls:
#         if dangercall.startswith('D'):
#             danger_flag = True
#             break
#     if not danger_flag:
#         return False
#     for dangercall in dangercalls:
#         type_num = dangercall[1:]
#         # if dangercall.startswith('D'):
#         #     return True
#
#         # if dangercall.startswith('A') or dangercall.startswith('C'):
#         #     source_apis.append(type_num)
#         #     if type_num == '02' and '01' in sink_apis:
#         #         return True
#         #     if type_num == '01' and ('03' in sink_apis or '04' in sink_apis):
#         #         return True
#         # if dangercall.startswith('B') or dangercall.startswith('C'):
#         #     sink_apis.append(type_num)
#         #     if type_num == '01' and '02' in source_apis:
#         #         return True
#         #     if (type_num == '03' or type_num == '04') and '01' in source_apis:
#         #         return True
#
#         if dangercall.startswith('A') or dangercall.startswith('C'):
#             source_apis.append(type_num)
#             if (type_num == '02' or type_num == '06' or type_num == '07') and ('01' in sink_apis or '05' in sink_apis):
#                 return True
#             if (type_num == '01' or type_num == '05') and ('03' in sink_apis or '04' in sink_apis or '08' in sink_apis):
#                 return True
#         if dangercall.startswith('B') or dangercall.startswith('C'):
#             sink_apis.append(type_num)
#             if (type_num == '01' or type_num == '05') and (
#                     '02' in source_apis or '06' in source_apis or '07' in source_apis):
#                 return True
#             if (type_num == '03' or type_num == '04' or type_num == '08') and (
#                     '01' in source_apis or '05' in source_apis):
#                 return True
#
#     # for dangercall in dangercalls:
#     #     type_num = dangercall[1:]
#     #     if dangercall.startswith('D'):
#     #         return True
#     #     if dangercall.startswith('A') or dangercall.startswith('C'):
#     #         source_apis.append(type_num)
#     #         if (type_num == '02' or type_num == '06' or type_num == '07') and '01' in sink_apis:
#     #             return True
#     #         if type_num == '01' and ('03' in sink_apis or '04' in sink_apis or '08' in sink_apis):
#     #             return True
#     #     if dangercall.startswith('B') or dangercall.startswith('C'):
#     #         sink_apis.append(type_num)
#     #         if type_num == '01' and '02' in source_apis:
#     #             return True
#     #         if (type_num == '03' or type_num == '04' or type_num == '08') and '01' in source_apis:
#     #             return True
#     return False
